package com.taotao.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EasyUIDataGridResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.IDUtils;
import com.taotao.common.utils.JsonUtils;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemExample;
import com.taotao.redis.JedisClient;
import com.taotao.service.ItemService;

import redis.clients.jedis.Jedis;

@Service
public class ItemServiceImpl implements ItemService{
	//注入dao
	@Autowired
	private TbItemMapper itemMapper;
	@Autowired
	private TbItemDescMapper descMapper;
	//注入消息队列模板对象
	@Autowired
	private JmsTemplate jmsTemplate;
	//注入消息队列Destination对象
	@Resource(name = "topicDestination")
	private Topic topic;
	//注入常量值
	@Value("${REDIS_ITEM_KEY}")
	private String REDIS_ITEM_KEY;
	@Value("${REDIS_ITEM_EXPIRE}")
	private Integer REDIS_ITEM_EXPIRE;
	//注入Jedis
	@Autowired
	private JedisClient jedisClient;
	@Override
	public EasyUIDataGridResult getItemList(Integer page, Integer rows) {
		//设置分页信息
		PageHelper.startPage(page, rows);
		//执行查询
		TbItemExample example = new TbItemExample();
		List<TbItem> ItemList = itemMapper.selectByExample(example);
		//取分页信息
		PageInfo<TbItem> pageInfo = new PageInfo<>(ItemList);
		//封装返回页信息
		EasyUIDataGridResult dataGridResult = new EasyUIDataGridResult();
		dataGridResult.setTotal(pageInfo.getTotal());
		dataGridResult.setRows(ItemList);
		return dataGridResult;
	}
	@Override
	public TaotaoResult addItem(TbItem tbItem, String desc) {
		//生成商品id
		final long id = IDUtils.genItemId();
		tbItem.setId(id);
		//商品状态，1-正常，2-下架，3-删除
		tbItem.setStatus((byte) 1);
		Date date = new Date();
		tbItem.setCreated(date);
		tbItem.setUpdated(date);
		//保存商品信息到数据库
		itemMapper.insert(tbItem);
		
		TbItemDesc itemDesc = new TbItemDesc();
		itemDesc.setItemId(id);
		itemDesc.setItemDesc(desc);
		itemDesc.setCreated(date);
		itemDesc.setUpdated(date);
		descMapper.insert(itemDesc);
		//将新添加的商品通过消息队列ActiveMQ添加到索引库
		jmsTemplate.send(topic, new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				TextMessage textMessage = session.createTextMessage(id+"");
				return textMessage;
			}
		});
		return TaotaoResult.ok();
	}
	@Override
	public TaotaoResult getItemDesc(Long itemId) {
		TbItemDesc tbItemDesc = descMapper.selectByPrimaryKey(itemId);
		return TaotaoResult.ok(tbItemDesc);
	}
	//更新商品
	@Override
	public TaotaoResult updateItem(TbItem tbItem,String desc) {
		//更新商品表
		TbItem _tbItem = itemMapper.selectByPrimaryKey(tbItem.getId());
		tbItem.setStatus((byte) 1);
		tbItem.setCreated(_tbItem.getCreated());
		tbItem.setUpdated(new Date());
		itemMapper.updateByPrimaryKey(tbItem);
		//更新商品描述表
		TbItemDesc itemDesc = descMapper.selectByPrimaryKey(tbItem.getId());
		itemDesc.setItemDesc(desc);
		itemDesc.setCreated(itemDesc.getCreated());
		itemDesc.setUpdated(new Date());
		descMapper.updateByPrimaryKeyWithBLOBs(itemDesc);
		return TaotaoResult.ok();
	}
	@Override
	public TbItem getItemById(Long itemId) {
		//查询缓存是否命中
		try {
			String json = jedisClient.get(REDIS_ITEM_KEY+":"+itemId+":BASE");
			if(StringUtils.isNotBlank(json)){
				return JsonUtils.jsonToPojo(json, TbItem.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		TbItem tbItem = itemMapper.selectByPrimaryKey(itemId);
		//添加商品后存入到缓存中
		try {
			
			jedisClient.set(REDIS_ITEM_KEY+":"+itemId+":BASE", JsonUtils.objectToJson(tbItem));
			//设置过期时间
			jedisClient.expire(REDIS_ITEM_KEY+":"+itemId+":BASE", REDIS_ITEM_EXPIRE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tbItem;
	}
	@Override
	public TbItemDesc getItemDescById(Long itemId) {
		//查询缓存是否命中
		try {
			
			String json = jedisClient.get(REDIS_ITEM_KEY+":"+itemId+":DESC");
			if(StringUtils.isNotBlank(json)){
				return JsonUtils.jsonToPojo(json, TbItemDesc.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		TbItemDesc itemDesc = descMapper.selectByPrimaryKey(itemId);
		//添加商品后存入到缓存中
		try {
			jedisClient.set(REDIS_ITEM_KEY+":"+itemId+":DESC", JsonUtils.objectToJson(itemDesc));
			//设置过期时间
			jedisClient.expire(REDIS_ITEM_KEY+":"+itemId+":DESC", REDIS_ITEM_EXPIRE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return itemDesc;
	}
}
